package cn.cdqf.coupon.controller;

import cn.cdqf.coupon.controller.validator.BindingResultUtil;
import cn.cdqf.coupon.entity.CouponTemplate;
import cn.cdqf.coupon.entity.param.CouponTemplateParam;
import cn.cdqf.coupon.service.CouponTemplateService;
import cn.cdqf.globle.common.ResultResponse;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Api("优惠券模板接口")
@RestController
public class CouponTemplateController {

    @Autowired
    private CouponTemplateService couponTemplateService;

    @PostMapping("couponTemplate")
    @ApiOperation("增加优惠券模板")
    //springmvc接收参数 只能有一个@RequestBody  而且 RequestBody不能跟requestparam共用
    public ResultResponse resultResponse(
            @ApiParam(value = "优惠券logo",name = "multipartFile",required = true) MultipartFile multipartFile,
            @ApiParam("模板接收实体类") @Validated  CouponTemplateParam couponTemplateParam, BindingResult bindingResult){
        BindingResultUtil.checkBindingResult(bindingResult);
        //todo 操作人员
        couponTemplateService.add(couponTemplateParam,multipartFile);
        return ResultResponse.success();
    }
    @ApiOperation("根据商家id与等级查找优惠券模板")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商家id",name="merchantId",required = true,type = "path")
    })
    //获得模板id
    @GetMapping("couponTemplate/{merchantId}/{level}")
    public ResultResponse getCouponTemplateMapByMAndLevel(
            @PathVariable("merchantId") String merchantId,
            @PathVariable("level") Integer level
    ){
       return  couponTemplateService.getCouponTemplateByMAndLevel(merchantId,level);
    }
    @ApiOperation("根据模板id,修改库存")
    @PatchMapping("couponTemplate/{couponId}/{count}")
    public ResultResponse decrByCouponId(
            @PathVariable("couponId") String couponId,
            @PathVariable("count") Integer count
    ){
        return couponTemplateService.decrByCouponId(couponId,count);
    }
    @GetMapping("couponTemplate/{couponId}")
    public ResultResponse getByCouponId(@PathVariable("couponId") String couponId){

        return couponTemplateService.getByCouponId(couponId);
    }
    @PatchMapping ("couponTemplate")
    public ResultResponse updateByCouponId(
            @ApiParam @RequestBody @Validated CouponTemplate couponTemplateParam, BindingResult bindingResult){
        //完成参数校验
        return couponTemplateService.updateByCouponId(couponTemplateParam);
    }
}
